package com.woniu.p2p.behindServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.BehindScoreService;
import com.woniu.p2p.entity.User;
import com.woniu.p2p.entity.UserScore;
import com.woniu.p2p.mapper.UserMapper;
import com.woniu.p2p.mapper.UserScoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 86152
 * @create 2020/8/13
 * @since 1.0.0
 */
@Service
public class BehindScoreServiceImp implements BehindScoreService {
    @Autowired
    UserScoreMapper userScoreMapper;
    /*条件查询*/
    @Override
    public PageInfo<UserScore> getscorebycons(String status, String keyword, Date starttime, Date endtime, Integer currentPage, int pagesize) {
        PageHelper.startPage(currentPage,pagesize);
        List<UserScore> userScores=userScoreMapper.getscorebycons(status,keyword,starttime,endtime);
        PageInfo<UserScore> userScorePageInfo = new PageInfo<>(userScores);
        return userScorePageInfo;
    }
    /*查询所有*/
    @Override
    public List<UserScore> selectAll() {
        return userScoreMapper.selectAll();
    }
    /*根据id删除积分记录并重新查询*/
    @Override
    @Transactional
    public PageInfo<UserScore> deletebyid(String status, String keyword, Date starttime, Date endtime, Integer currentPage, int pagesize, Integer scoreId) {
        userScoreMapper.deleteByPrimaryKey(scoreId);
        PageHelper.startPage(currentPage,pagesize);
        List<UserScore> scoreList=userScoreMapper.getscorebycons(status,keyword,starttime,endtime);
        PageInfo<UserScore> scorePageInfo = new PageInfo<>(scoreList);
        return scorePageInfo;
    }
    /*根据id查询单条记录*/
    @Override
    public UserScore getscoredetailsbyid(Integer scoreId) {
        return userScoreMapper.selectByPrimaryKey(scoreId);
    }

    @Autowired
    UserMapper userMapper;
    /*查询所有的用户id*/
    @Override
    public List<Integer> getuseridlist() {
        return userMapper.getuseridlist();
    }
    /*更新用户积分并添加积分变更记录*/
    @Override
    @Transactional
    public int updatescoreinuserandinsertuserscore(Integer scoreAmount, Integer userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        int row=userMapper.updatescorebyid(scoreAmount,userId);
        UserScore userScore = new UserScore();
        userScore.setScoreAmount(user.getUserIntergal());
        userScore.setScoreContext("+"+scoreAmount);
        userScore.setScoreBalance(user.getUserIntergal()+scoreAmount);
        userScore.setScoreUname(user.getUserAccount());
        userScore.setScoreStatus("增加");
        userScore.setUserid(userId);
        userScore.setScoreTime(new Date());
        int insert = userScoreMapper.insert(userScore);
        return row+insert;
    }
    /*批量删除*/
    @Override
    @Transactional
    public int piliangdel(int[] ids) {
        int row=0;
        for(int i:ids){
            int i1 = userScoreMapper.deleteByPrimaryKey(i);
            row+=i1;
        }
        return row;
    }
}
